#include "Test.Font.h"

#undef  APP_NAME
#define APP_NAME	TestSensorRotation
#undef  APP_BASE
#define APP_BASE	TestFont


class APP_NAME : public APP_BASE
{
private:
	typedef Primitives::Parallelogram2D		Parallelogram2D;
	typedef Parallelogram2D::vertex_t		Vertex2D;			

	typedef SHARED_POINTER_TYPE( APP_NAME )	TestSensorRotationPtr;

private:
	vec3	rot;
	/*
	static const ESensor::type		sensor		= ESensor::ROTATION;
	static const ESystemEvent::type	eventType	= ESystemEvent::ROTATION;
#	define EVENT_DATA				rotation
	*/
	static const ESensor::type		sensor		= ESensor::MAGNETIC_FIELD;
	static const ESystemEvent::type	eventType	= ESystemEvent::MAGNETIC;
#	define EVENT_DATA				magnetic
	/*
	static const ESensor::type		sensor		= ESensor::ORIENTATION;
	static const ESystemEvent::type	eventType	= ESystemEvent::ORIENTATION;
#	define EVENT_DATA				orient
	*/

public:
	APP_NAME (IPlatform *platform, EDevice::type deviceType) :
		APP_BASE( platform, deviceType )
	{
		ESS()->GetEventSystem()->AttachEventListener(
			DelegateBuilder::Create( TestSensorRotationPtr(this), &APP_NAME::_OnRotation ),	eventType );
	}


	~APP_NAME ()
	{
		ESS()->GetEventSystem()->DetachEventListener(
			DelegateBuilder::Create( TestSensorRotationPtr(this), &APP_NAME::_OnRotation ),	eventType );
	}


	void Update (float dt) override
	{
		TestApp::Update( dt );
		
		ESS()->GetGraphicsEngine()->GetViewport()->Apply();
		ESS()->GetGLStateManager()->ClearColor( color4f( 0.0f ) );
		
		const vec2 &			aspect	= ESS()->GetGraphicsEngine()->GetViewport()->GetResCorrectAspect();
		const float				r = 0.6f;
		const TCircle<float>	c( r );
		
		// rotation
		//const float				a = rot.z * Pi<float>();
		
		//rot = vec3( -5.0f, 3.0f, 60.0f );

		// magnetic
		
		const vec3				n = rot.Normalized();
		const line2d			line( vec2(), n.xy().Normalized() * r * aspect );
		const float				a = line.AngleBetweenLines( line2d( vec2(0.0f), vec2(1.0f, 0.0f) * r * aspect ) ) *
									Sign( line.End().y );
		

		// orientation
		//const float		a = rot.x;	//DTR( rot.y );

		const vec2				p0 = c.GetPoint( a - 0.1f ) * aspect;
		const vec2				p1 = c.GetPoint( a + 0.1f ) * aspect;
		const color4u			cl( 255, 0, 0, 255 );

		ESS()->GetGraphicsEngine()->GetRender2D().DrawCircle( material, vec2(0.0f), r, 36, color4u(255), true );
		
		BatchRenderPtr	br		= ESS()->GetGraphicsEngine()->GetBatchRender();

		br->AddBatch( Parallelogram2D(	Vertex2D( vec2(), vec2(), cl ),
										Vertex2D( vec2(), vec2(), cl ),
										Vertex2D( p0, vec2(), cl ),
										Vertex2D( p1, vec2(), cl ) ) );
		
		fontBatch.Draw( vec2(-0.2f, -0.7f), (string().FormatF( a, StringFormatF().Fmt(0,2) )) );

		batch.Flush();
		batch.Clear();
		
		ESS()->GetGraphicsEngine()->SwapBuffers();
	}
	

	void OnEnter () override
	{
		APP_BASE::OnEnter();

		material.SetColorState( RenderState::ColorState() );

		if ( sensor == ESensor::ORIENTATION )
		{
			GetPlatform()->EnableSensor( ESensor::ORIENTATION );
			GetPlatform()->EnableSensor( ESensor::ACCELEROMETER );
			GetPlatform()->EnableSensor( ESensor::MAGNETIC_FIELD );
		}
		else
			GetPlatform()->EnableSensor( sensor );
	}

	
	void _OnRotation (const SysEvent &ev)
	{
		vec3	v( ev.EVENT_DATA.x, ev.EVENT_DATA.y, ev.EVENT_DATA.z );

		rot = v;

		DEBUG_CONSOLE( (string("vec - x:") << v.x << ", y:" << v.y << ", z:" << v.z).cstr() );
	}
};